<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="菜品图片" >
      <el-upload
        class="avatar-uploader"
        :action="url"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      name="file">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      </el-form-item>
      <el-form-item label="菜品名称" prop="dishName">
      <el-input v-model="dataForm.dishName" placeholder="菜品名称"></el-input>
    </el-form-item>
    <el-form-item label="菜品价格" prop="dishPrice">
      <el-input v-model="dataForm.dishPrice" placeholder="菜品价格"></el-input>
    </el-form-item>
      <el-form-item label="菜品类别" prop="dishType">
        <el-select v-model="dataForm.dishType" placeholder="请选择">
          <el-option label="主食" value="主食"></el-option>
          <el-option label="菜" value="菜"></el-option>
          <el-option label="汤" value="汤"></el-option>
        </el-select>
      </el-form-item>
    <el-form-item label="禁忌人群" prop="dishTaboo">
      <el-input v-model="dataForm.dishTaboo" placeholder="禁忌人群"></el-input>
    </el-form-item>
    <el-form-item label="食材说明" prop="dishIngred">
      <el-input v-model="dataForm.dishIngred" placeholder="食材说明"></el-input>
    </el-form-item>
<!--    <el-form-item label="食材图片" prop="dishPhoto">-->
<!--      <el-input v-model="dataForm.dishPhoto" placeholder="食材图片"></el-input>-->
<!--    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false,imageUrl = ''">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<script>


  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          dishName: '',
          dishPrice: '',
          dishType: '',
          dishTaboo: '',
          dishIngred: '',
          dishPhoto: '',
          createTime: '',
          updateTime: ''
        },
        imageUrl: '',
        dataRule: {
          dishName: [
            { required: true, message: '菜品名称不能为空', trigger: 'blur' }
          ],
          dishPrice: [
            { required: true, message: '菜品价格不能为空', trigger: 'blur' }
          ],
          dishType: [
            { required: true, message: '菜品类别 主食汤菜不能为空', trigger: 'blur' }
          ],
          dishTaboo: [
            { required: true, message: '禁忌人群不能为空', trigger: 'blur' }
          ],
          dishIngred: [
            { required: true, message: '食材说明不能为空', trigger: 'blur' }
          ],
          dishPhoto: [
            { required: true, message: '食材图片不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        },
        url: ''
      }
    },
    methods: {
      init (id) {
        this.url = this.$http.adornUrl(`/tb_bs_dish/bsdish/dishUpload?token=${this.$cookie.get('token')}`)
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tb_bs_dish/bsdish/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.dishName = data.bsDish.dishName
                this.dataForm.dishPrice = data.bsDish.dishPrice
                this.dataForm.dishType = data.bsDish.dishType
                this.dataForm.dishTaboo = data.bsDish.dishTaboo
                this.dataForm.dishIngred = data.bsDish.dishIngred
                this.dataForm.dishPhoto = data.bsDish.dishPhoto
                this.dataForm.createTime = data.bsDish.createTime
                this.dataForm.updateTime = data.bsDish.updateTime
                if (id > 0){
                  this.imageUrl = data.bsDish.dishPhoto
                  console.log(data.bsDish.dishPhoto)
                  console.log("进来了")
                }
              }
            })
          }
        })
        console.log(id > 0 ? "1" : "2")
        console.log("要修改的编号：：" + id)
        console.log(this.dataForm)
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tb_bs_dish/bsdish/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'dishName': this.dataForm.dishName,
                'dishPrice': this.dataForm.dishPrice,
                'dishType': this.dataForm.dishType,
                'dishTaboo': this.dataForm.dishTaboo,
                'dishIngred': this.dataForm.dishIngred,
                'dishPhoto': this.dataForm.dishPhoto,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
                this.imageUrl = ''
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      handleAvatarSuccess(res, file) {
        console.log("handleAvatarSuccess")
        this.imageUrl = URL.createObjectURL(file.raw)
        this.dataForm.dishPhoto = res.msg
        console.log(this.imageUrl)
      },
      beforeAvatarUpload(file) {
        console.log("beforeAvatarUpload")
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return isJPG && isLt2M
      }
    }
  }
</script>
